package com.yvon.service.demo.spring.context.support;

import com.yvon.service.demo.spring.beans.BeansException;
import com.yvon.service.demo.spring.beans.factory.ConfigurableListableBeanFactory;
import com.yvon.service.demo.spring.beans.factory.support.DefaultListableBeanFactory;

/**
 * 可刷新应用程序上下文抽象类
 *
 * @author : Yvon
 * @since : 2022-12-07
 */
public abstract class AbstractRefreshableApplicationContext extends AbstractApplicationContext{

    private DefaultListableBeanFactory beanFactory;

    @Override
    protected void refreshBeanFactory() throws BeansException {
        // 创建BeanFactory
        DefaultListableBeanFactory beanFactory = createBeanFactory();
        // 加载BeanDefinition
        loadBeanDefinitions(beanFactory);
        this.beanFactory = beanFactory;
    }

    /**
     * 创建bean工厂
     * 实例化DefaultListableBeanFactory
     *
     * @return {@code DefaultListableBeanFactory }
     */
    private DefaultListableBeanFactory createBeanFactory() {
        return new DefaultListableBeanFactory();
    }

    /**
     * 获得bean工厂
     * 实现于AbstractApplicationContext
     *
     * @return {@code ConfigurableListableBeanFactory }
     */
    @Override
    protected ConfigurableListableBeanFactory getBeanFactory() {
        return beanFactory;
    }

    /**
     * 加载bean定义
     * Bean对象的定义和注册，实现了BeanFactoryPostProcess接口，BeanPostProcess接口对Bean信息的配置
     *
     * @param beanFactory bean工厂
     */
    protected abstract void loadBeanDefinitions(DefaultListableBeanFactory beanFactory);
}
